package com.cs.user.domain.service.impl;

import cn.dev33.satoken.stp.StpUtil;
import com.cs.user.common.bos.AuthUserBO;
import com.cs.user.common.entity.BaseResponse;
import com.cs.user.common.entity.ErrorCode;
import com.cs.user.common.entity.PageResult;
import com.cs.user.common.exception.BusinessException;
import com.cs.user.common.utlis.ThrowUtils;
import com.cs.user.domain.bos.UserBO;
import com.cs.user.domain.bos.UserRoleBO;
import com.cs.user.domain.service.UserDomainService;
import com.cs.user.domain.service.UserRoleDomainService;
import com.cs.user.infra.basic.entity.User;
import com.cs.user.infra.basic.entity.UserRole;
import com.cs.user.infra.basic.service.UserService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class UserDomainServiceImpl implements UserDomainService {

    @Resource
    private UserRoleDomainService userRoleDomainService;

    @Resource
    private UserService userService;

    @Override
    @Transactional(rollbackFor = BusinessException.class)
    public Boolean register(AuthUserBO authUserBO) {
        // 类型转换
        User user = new User();
        BeanUtils.copyProperties(authUserBO, user);
        // 注册
        Boolean uFlag = userService.register(user);
        ThrowUtils.throwIf(!uFlag, ErrorCode.PARAMS_ERROR,"注册失败");
        // 为新用户分配默认角色
        // 获取用户信息
        UserBO userBO = new UserBO();
        BeanUtils.copyProperties(user, userBO);
        UserBO userBo = queryUserByAccount(userBO);
        //TODO 获取角色信息

        // 设置参数
        UserRoleBO userRoleBO = new UserRoleBO();
        userRoleBO.setUserId(userBo.getBusinessId());
        userRoleBO.setRoleId(1838462796387991554L);
        // 向关系表添加数据
        Boolean urFlag = userRoleDomainService.addUserRole(userRoleBO);
        ThrowUtils.throwIf(!urFlag, ErrorCode.PARAMS_ERROR,"添加用户角色关系失败");
        return true;
    }

    @Override
    public AuthUserBO login(AuthUserBO authUserBO) {
        //类型转换
        User user = new User();
        BeanUtils.copyProperties(authUserBO, user);
        //调用infra层方法
        User loginUser = userService.login(user);
        ThrowUtils.throwIf(loginUser == null, ErrorCode.PARAMS_ERROR,"账号或密码错误");
        //类型转换
        AuthUserBO authUserBo = new AuthUserBO();
        //执行登录
        StpUtil.login(loginUser.getBusinessId());
        //设置返回值
        authUserBo.setBusinessId(loginUser.getBusinessId());
        authUserBo.setToken(StpUtil.getTokenValue());
        return authUserBo;
    }

    @Override
    public Boolean updateUser(UserBO userBO) {
        //参数校验
        ThrowUtils.throwIf(userBO.getBusinessId() == null, ErrorCode.PARAMS_ERROR,"未找到用户id");
        //类型转换
        User user = new User();
        BeanUtils.copyProperties(userBO, user);
        //调用infra层方法
        return userService.updateUser(user);
    }

    @Override
    public Boolean deleteUser(UserBO userBO) {
        // 参数校验
        ThrowUtils.throwIf(userBO.getBusinessId() == null, ErrorCode.PARAMS_ERROR,"未找到用户id");
        // 类型转换
        User user = new User();
        BeanUtils.copyProperties(userBO, user);
        //调用infra层方法
        return userService.deleteUser(user);

    }

    @Override
    public PageResult<UserBO> pageUser(UserBO userBO) {
        // 创建请求返回结果
        PageResult<UserBO> pageResult = new PageResult<>();
        // 设置请求参数
        pageResult.setPageNo(userBO.getPageNo());
        pageResult.setPageSize(userBO.getPageSize());
        int start = (userBO.getPageNo() - 1) * userBO.getPageSize();
        int pageSize = userBO.getPageSize();
        // 类型转换
        User user = new User();
        BeanUtils.copyProperties(userBO, user);
        // 调用infra层方法
        List<User> subjectInfoList = userService.queryPage(user, start, pageSize);
        // 类型转换
        List<UserBO> userBOList = subjectInfoList.stream().map(item -> {
            UserBO userBo = new UserBO();
            BeanUtils.copyProperties(item, userBo);
            return userBo;
        }).collect(Collectors.toList());
        // 设置返回结果
        pageResult.setRecords(userBOList);
        pageResult.setTotal(userBOList.size());
        return pageResult;
    }

    @Override
    public UserBO queryUserByAccount(UserBO userBO) {
        // 类型转换
        User user = new User();
        BeanUtils.copyProperties(userBO, user);
        // 调用infra层方法
        User result = userService.queryUserByAccount(user);
        // 类型转换
        UserBO userBo = new UserBO();
        BeanUtils.copyProperties(result, userBo);
        return userBo;
    }
}
